import Vue from "vue";
import VueRouter from "vue-router";

import { getToken } from "@/utils/storage";

import Layout from "@/views/layout.vue";
import Login from "@/views/login.vue";
import Map from "@/views/map.vue";
import Home from "@/views/home.vue";
import List from "@/views/list.vue";
import News from "@/views/news.vue";
import Profile from "@/views/profile.vue";
import Register from "@/views/register.vue";
import Favorate from "@/views/favorate.vue";
import City from "@/views/city.vue";
import Rend from "@/views/rend.vue";

import { Toast } from "vant";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    component: ()=> import('@/views/login'),
  },
  {
    path: "/map",
    component: ()=> import('@/views/map'),
  },
  {
    path: "/register",
    component: ()=> import('@/views/register'),
  },
  {
    path: "/favorate",
    component: ()=> import('@/views/favorate'),
  },
  {
    path: "/city",
    component: ()=> import('@/views/city'),
  },
  {
    path: "/rend",
    component: ()=> import('@/views/rend'),
  },
  {
    path: "/list",
    component: ()=> import('@/views/list'),
  },

  {
    path: "/",
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: "/home",
        component: ()=> import('@/views/home'),
        children: [
          {
            path: "/list",
            component: ()=> import('@/views/list'),
          },
        ],
      },
      {
        path: "/news",
        component: ()=> import('@/views/news'),
      },
      {
        path: "/profile",
        component: ()=> import('@/views/profile'),
      },
    ],
  },
];

const router = new VueRouter({
  routes,
});

const whiteList = ["/login", "/register"];

router.beforeEach((to, from, next) => {
  if (getToken()) {
    next();
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      Toast.fail("游客请登录");
      next("/login");
    }
  }
});
export default router;
